package com.planesnet.gps1;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class FastLocationProvider extends Thread {
	public static boolean DEBUG = true;
	public static String TAG = "LOC";
	private static int STATUS_INTERVAL = 2000; // milisegundos.

	private boolean enabled = false;
	private Context context;
	private boolean requestCancel;
	LocationManager locationManager;
	Location currentLocation = null;
	Location location;
	FastLocationListener gpsListener,networkListener;
	
	LocationListener userListener;
	
	
	private static long minTime = 0;
	private static float minDistance=0;

	public FastLocationProvider(Context context) {
		super();
		this.context = context;

		debug("Init fast provider");
		locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

		if (locationManager != null) {
			
			
			// Inicializar el network location service.
			if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
				networkListener = new FastLocationListener();
				locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,networkListener);
			}
			
			if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
				gpsListener = new FastLocationListener();
				locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,minTime, minDistance,gpsListener);
			}

			
		}

		enabled = true;
		
	}
	
	public void setLocationListener(LocationListener userListener) {
		this.userListener = userListener;
	}
	

	public void run() {

		debug("Init provider:");
		
		requestCancel = false;
		while (enabled && !requestCancel) {

			
			try {
				Thread.sleep(STATUS_INTERVAL);
			} catch (InterruptedException e) {
			}
			debug(currentLocation);

		}

		locationManager.removeUpdates(gpsListener);
		locationManager.removeUpdates(networkListener);
		gpsListener = null;
		networkListener = null;
		locationManager = null;

		debug("Stop location.");
		enabled = false;

	}

	public boolean isEnabled() {
		return enabled;
	}

	public void requestCancel() {
		this.requestCancel = true;
		debug("Request cancel");
	}

	private class FastLocationListener extends AdapterLocation {
		@Override
		public void onLocationChanged(Location loc) {
			if (loc != null) {
				setCurrentLocation(loc);
				
				// Generar los eventos a usuarios.
				if(userListener!=null) userListener.onLocationChanged(loc);
			}
		}

		@Override
		public void onProviderDisabled(String provider) {
			debug(provider + ": disabled.");
			// Generar los eventos a usuarios.
			if(userListener!=null) userListener.onProviderDisabled(provider);

		}

		@Override
		public void onProviderEnabled(String provider) {
			debug(provider + ": enabled.");
			// Generar los eventos a usuarios.
			if(userListener!=null) userListener.onProviderEnabled(provider);

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			debug("Status changed: " + status);
			// Generar los eventos a usuarios.
			if(userListener!=null) userListener.onStatusChanged(provider, status, extras);


		}
	}

	public void setCurrentLocation(Location loc) {
		currentLocation = loc;
		debug(loc);

	}

	public Location getCurrentLocation() {
		return currentLocation;
	}

	private void debug(String s) {
		if (DEBUG) {
			Log.i(TAG, s);
		}
	}

	private void debug(Location loc) {
		if (DEBUG) {
			if (loc != null) {
				Log.i(TAG, "Latitud  : " + loc.getLatitude());
				Log.i(TAG, "Longitud : " + loc.getLongitude());
			}
		}
	}

}
